﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Pavilion
{
    public abstract class GameToolBase : IPropertyChanged
    {
        public event PropertyChangedHandler PropertyChanged;
        private Dictionary<string, Action<object>> dataExecutionDictionary;
        protected Dictionary<string, Action<object>> DataExecutionDictionary { get { return (dataExecutionDictionary == null) ? dataExecutionDictionary = new Dictionary<string, Action<object>>() : dataExecutionDictionary; } }

        public abstract void Initialize(GameItem gameItem);
        public abstract void Deserialize(XElement xmlProperties);
        public abstract void Serialize();

        public virtual void BeginDataUpdate() { }
        public virtual void EndDataUpdate() { }

        public void UpdateData(GameData gameData)
        {
            BeginDataUpdate();

            foreach (string key in gameData.PropertyDictionary.Keys)
                DataExecutionDictionary[key].Invoke(gameData.PropertyDictionary[key]);

            EndDataUpdate();
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, propertyName);
        }
    }
}
